package com.weather.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.weather.domain.User;
import com.weather.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/system/user")
public class UserController {

    @Autowired
    private UserService UserService;

    /**
     * 分页查询用户列表
     */
    @GetMapping("/list")
    public Page<User> list(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            User sysUser // 可选条件过滤（例如用户名、状态等）
    ) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (sysUser.getUserName() != null) {
            queryWrapper.lambda().like(User::getUserName, sysUser.getUserName());
        }
        if (sysUser.getStatus() != null) {
            queryWrapper.lambda().eq(User::getStatus, sysUser.getStatus());
        }
        return UserService.page(new Page<>(pageNum, pageSize), queryWrapper);
    }

    /**
     * 获取单个用户详情
     */
    @GetMapping("/{userId}")
    public User getById(@PathVariable Long userId) {
        return UserService.getById(userId);
    }

    /**
     * 添加用户
     */
    @PostMapping
    public boolean add(@RequestBody User sysUser) {
        return UserService.save(sysUser);
    }

    /**
     * 修改用户
     */
    @PutMapping
    public boolean update(@RequestBody User sysUser) {
        return UserService.updateById(sysUser);
    }

    /**
     * 删除用户（支持批量）
     */
    @DeleteMapping("/{userIds}")
    public boolean delete(@PathVariable List<Long> userIds) {
        return UserService.removeByIds(userIds);
    }
}
